$(function () {
    $('input').iCheck({
        checkboxClass: 'icheckbox_minimal-tpv',
        radioClass: 'iradio_minimal-tpv',
        increaseArea: '20%'
    });

    $("input[type=radio][name=moderator]").on('ifChecked', function(event){
        var moderator = $(this).val();
        if (moderator == 'true') {
            $(".meeting-join-moderator-password").css({ display: "flex" });
        } else {
            $(".meeting-join-moderator-password").css({ display: "none" });
            $("input[name='moderatorPassword']").val("");
        }
    });

    $('#attendee').iCheck('check');

    $("#joinButton").click(function() {
        var $joinButton = $("#joinButton");

        var meetingId = $("input[name='meetingId']").val();
        var meetingPassword = $("input[name='meetingPassword']").val();
        var moderatorPassword = $("input[name='moderatorPassword']").val();
        var name = $("input[name='name']").val();
        var moderator = $('input[type=radio][name=moderator]:checked').val();

        if(meetingId != undefined && meetingId.length < 6) {
            $MB.n_warning($MB.i18n('meeting.tips.not.found'));
            return;
        }

        if(moderator == 'true' && moderatorPassword.length < 6) {
            $MB.n_warning($MB.i18n('meeting.tips.moderator.password.error'));
            return;
        }

        $joinButton.html("").append("<div class='login-loder'><div class='line-scale'><div></div><div></div><div></div><div></div><div></div></div></div>");

        $.ajax({
            type : "post",
            url : "/meeting/join?meetingId=" + meetingId,
            cache : false,
            data : {
               "name" : name,
               "meetingPassword" : meetingPassword,
               "moderatorPassword" : moderatorPassword,
               "moderator" : moderator
            },
            success : function(r) {
                $joinButton.html("加入会议");

                if(r.code == 0) {
                    window.location = r.msg.url;
                } else {
                    if(r.code == 609) {//locked
                        showMeetingLock(r.id, 'guest-' + new Date().getTime(), name);
                    } else {
                        $MB.n_warning($MB.i18n(r.msg));
                    }

                }
            }
        });

    });
});
